package com.ftg.learn.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * 通用控制器
 *
 *  stream 线程池
 *
 * @param <E> 表示service
 * @param <T> 实休类
 * @author kangjx
 * @version 1.0
 * @date 2023/9/20 下午1:58
 */

public class BaseController<E extends IService<T>, T> {

    @Autowired
    private E service;

    @ApiOperation("分页功能")
    @GetMapping("/showall")
    public IPage<T> showall(@RequestParam("start") int start, @RequestParam("limit") int limit) {
        IPage<T> ipage = new Page<>(start, limit);
        return service.page(ipage);
    }

    @ApiOperation("保存功能")
    @PutMapping("/saveByEntity")
    public RespMessUtil<Boolean> saveByEntity(@RequestBody T t) {
        boolean num = service.save(t);
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, num);
    }

    @ApiOperation("删除功能")
    @PutMapping("/modifyByEntity")
    public RespMessUtil<Boolean> modifyByEntity(@RequestBody T t) {
        boolean num = service.updateById(t);
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, num);
    }

    @DeleteMapping("/delByEntity/{id}")
    @ApiOperation("删除功能")
    public RespMessUtil<Boolean> delByEntity(@PathVariable("id") Serializable id) {
        boolean num = service.removeById(id);
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, num);
    }

    @ApiOperation("主键查询一条功能")
    @GetMapping("/findOne/{id}")
    public RespMessUtil<T> findOneByid(@PathVariable("id") Serializable id) {
        T entity = service.getById(id);
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, entity);
    }

}
